import java.util.Arrays;

class MyPrioirityQueue{
    int[] array;
    int usedSize;
    public MyPrioirityQueue(){
        array = new int[10];
    }
    public void createdHeap(int[] arr){
        if(arr==null){
            return ;
        }
        for(int i = 0;i<arr.length;i++){
            offer(arr[i]);
        }
    }
    public void offer(int val){
        if(isFull()){
            array = Arrays.copyOf(array,2*array.length);
        }
        array[usedSize] = val;
        shiftUp(usedSize);
        usedSize++;
    }
    private void shiftDown(int parent){
        int child = parent*2+1;
        while(child<usedSize-1){
            if(child+1<(usedSize-1)&&array[child+1]<array[child]){
                child++;
            }

            if(array[parent]>array[child]){
                swap(child,parent);
                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }


    }
    private void shiftUp(int child){
        int parent = (child-1)/2;
        while(child>=0){
            if(array[child]<array[parent]){
              swap(child,parent);
                child = parent;
                parent = (child-1)/2;
            }else{
                break;
            }
        }
    }
    public boolean isFull(){
        return usedSize==array.length;
    }
    public int poll(){
        int tem = array[0];
        array[0] = array[usedSize-1];
        array[usedSize-1] = tem;
        usedSize--;
        shiftDown(0);
        return tem;
    }
    private void swap(int x,int y){
        array[x] = array[x]^array[y];
        array[y] = array[x]^array[y];
        array[x] = array[x]^array[y];
    }
}


public class TextDemo {
    public static void main(String[] args) {
        MyPrioirityQueue my = new MyPrioirityQueue();
        int[] arr = new int[]{4,2,7,2,2,8,3,0,23,75,86,11};
        my.createdHeap(arr);
        System.out.println(Arrays.toString(my.array));
    }

}
